/* 画图 直线，园，多边形*/
import Draw from "ol/interaction/Draw";
import {polygonStyle,lineStyle} from './style';
import {unByKey} from 'ol/Observable';

class PaintTools{
  constructor(map) {
    this.map=null;
    this.source=null; //对应me.all_obj.monitor.data_c

    this.drawTool=null;

    this.event=null;
  }

  setMap(map){this.map=map;}
  setSource(source){this.source=source;}

  /**
   * 开始画图
   * @param type 'Circle','Polygon'
   */
  start=(type)=>{
    const that=this;
    let style = null;
    if (type=='Polygon'){
      style = polygonStyle;
    }else if (type=='Circle'){
      style = lineStyle;
    }else if (type=='LineString'){
      style = lineStyle;
    }
    this.drawTool = new Draw({
      type: type,
      source: this.source,
      style: style,
    });
    this.map.addInteraction(this.drawTool);
    this.drawTool.on('drawend', function(event) {
      event.feature.setStyle(style);
      event.feature.type = type;
      event.feature.type_id = type;

      that.event = event;

      that.finish();
    });
    //click,,singleclick
    //let dbclickEvent = me.map.on('dblclick', function(evt) {
    //    me.fence_draw_end(dbclickEvent);
    //});
  }

  finish=()=>{
    this.map.removeInteraction(this.drawTool);
    this.drawTool = null;
    //me.tt.obj.helpTooltipElement.innerHTML="";
    if (this.event){unByKey(this.event)}
  }
}

export default PaintTools;
